<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="null">
    <meta name="keyword" content="null">
    <meta name="theme-color" content="#600090">
    <meta name="msapplication-navbutton-color" content="#600090">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="#600090">
    <link rel="shortcut icon" href="/assets/favicon.ico">
    <link rel="alternate" type="application/atom+xml" title="张小阳" href="/atom.xml">
    <link rel="stylesheet" href="https://cdn.bootcss.com/animate.css/3.5.2/animate.css">
    <link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.css">
    <title>
        
        Vue父子组件通信实例｜MillZhang&#39;s blog
        
    </title>

    <link rel="canonical" href="http://millzhang.github.io/2017/06/26/2017-note/20.Vue父子组件通信实例/">

    <!-- Bootstrap Core CSS -->
    <link rel="stylesheet" href="/css/bootstrap.min.css">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/css/blog-style.css">

    <!-- Pygments Github CSS -->
    <link rel="stylesheet" href="/css/syntax.css">
</head>

<!-- hack iOS CSS :active style -->
<body ontouchstart="" class="animated fadeIn">
<!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top " id="nav-top" data-ispost = "true" data-istags="false
" data-ishome = "false" >
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand animated pulse" href="/">
                <span class="brand-logo">
                    MillZhang
                </span>
            </a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <!-- Known Issue, found by Hux:
            <nav>'s height woule be hold on by its content.
            so, when navbar scale out, the <nav> will cover tags.
            also mask any touch event of tags, unfortunately.
        -->
        <!-- /.navbar-collapse -->
        <div id="huxblog_navbar">
            <div class="navbar-collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href="/"><i class="fa fa-home"></i>主页</a>
                    </li>
                   <li>
                        <a href="/tags"><i class="fa fa-tags"></i>归档</a>
                    </li>
                    <li>
                        <a href="/about"><i class="fa fa-user"></i>关于</a>
                    </li>
                </ul>
            </div>
        </div>
    </div>
    <!-- /.container -->
</nav>
<script>
    // Drop Bootstarp low-performance Navbar
    // Use customize navbar with high-quality material design animation
    // in high-perf jank-free CSS3 implementation
//    var $body   = document.body;
    var $toggle = document.querySelector('.navbar-toggle');
    var $navbar = document.querySelector('#huxblog_navbar');
    var $collapse = document.querySelector('.navbar-collapse');

    $toggle.addEventListener('click', handleMagic)
    function handleMagic(e){
        if ($navbar.className.indexOf('in') > 0) {
        // CLOSE
            $navbar.className = " ";
            // wait until animation end.
            setTimeout(function(){
                // prevent frequently toggle
                if($navbar.className.indexOf('in') < 0) {
                    $collapse.style.height = "0px"
                }
            },400)
        }else{
        // OPEN
            $collapse.style.height = "auto"
            $navbar.className += " in";
        }
    }
</script>

<!-- Main Content -->

<!--only post-->

<header class="intro-header">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1 text-center">
                <div class="post-heading">
                    <h1>Vue父子组件通信实例</h1>
                    
                    <span class="meta">
                         作者 MillZhang
                        <span>
                          日期 2017-06-26
                         </span>
                    </span>
                    <div class="tags text-center">
                        
                        <a class="tag" href="/tags/#vue"
                           title="vue">vue</a>
                        
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="post-title-haojen">
        <span>
            Vue父子组件通信实例
        </span>
    </div>
    <div class="post-header-image">
        
            <img src="http://oritfw5nq.bkt.clouddn.com/wall-303.jpg?imageView2/0/format/jpg/interlace/1/q/85|watermark/2/text/QnkgTWlsbHpoYW5n/font/5b6u6L2v6ZuF6buR/fontsize/480/fill/I0ZERkRGRA==/dissolve/56/gravity/SouthEast/dx/10/dy/10|imageslim">
         
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">
            <!-- Post Container -->
            <div class="col-lg-8 col-lg-offset-1 col-sm-9 post-container">
                <h3 id="父组件向子组件传递值"><a href="#父组件向子组件传递值" class="headerlink" title="父组件向子组件传递值"></a>父组件向子组件传递值</h3><p>通过在父组件中,使用自定义属性来声明父组件需要传递的数据,子组件中则,显示的使用<code>props</code>来获取数据.</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!--父组件--&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></div><div class="line">    <span class="comment">&lt;!-- 一个封装抽象的对象上传组件--&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">common-uploader</span> @<span class="attr">imageChange</span>=<span class="string">"handleImageChange"</span> <span class="attr">:size</span>=<span class="string">"0.5"</span> <span class="attr">:image</span>=<span class="string">"form.img"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">common-uploader</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></div><div class="line"></div><div class="line"><span class="comment">&lt;!-- 子组件 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">section</span> <span class="attr">class</span>=<span class="string">"common-uploader"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">el-button</span> <span class="attr">type</span>=<span class="string">""</span> <span class="attr">id</span>=<span class="string">"uploadFile"</span>&gt;</span>点击上传<span class="tag">&lt;/<span class="name">el-button</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">el-progress</span> <span class="attr">:percentage</span>=<span class="string">"uploadPercent"</span> <span class="attr">:status</span>=<span class="string">"uploadStatus"</span> </span></div><div class="line">    <span class="attr">style</span>=<span class="string">"width:460px;margin-top:12px;"</span> <span class="attr">v-if</span>=<span class="string">"uploadLoading"</span>&gt;<span class="tag">&lt;/<span class="name">el-progress</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"bannerImageContainer"</span> <span class="attr">v-if</span>=<span class="string">"currentImage!=''"</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">img</span> <span class="attr">:src</span>=<span class="string">"currentImage"</span> <span class="attr">alt</span>=<span class="string">""</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">i</span> <span class="attr">class</span>=<span class="string">"el-icon-circle-cross delete"</span> @<span class="attr">click</span>=<span class="string">"currentImage=''"</span>&gt;</span><span class="tag">&lt;/<span class="name">i</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">form</span> <span class="attr">id</span>=<span class="string">"form"</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">input</span> <span class="attr">id</span>=<span class="string">"file"</span> <span class="attr">type</span>=<span class="string">"file"</span> <span class="attr">style</span>=<span class="string">"display:none;"</span> <span class="attr">accept</span>=<span class="string">"image/*"</span> /&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">form</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">section</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></div></pre></td></tr></table></figure>
<p>子组件js:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</div><div class="line">  <span class="attr">name</span>: <span class="string">'common-uploader'</span>,</div><div class="line">  data() &#123;</div><div class="line">    <span class="keyword">return</span> &#123;</div><div class="line">      <span class="attr">currentImage</span>: <span class="string">''</span>,</div><div class="line">      <span class="attr">uploadPercent</span>: <span class="number">0</span>,</div><div class="line">      <span class="attr">uploadStatus</span>: <span class="string">''</span>,</div><div class="line">      <span class="attr">uploadLoading</span>: <span class="literal">false</span>,</div><div class="line">    &#125;</div><div class="line">  &#125;,</div><div class="line">  created() &#123;</div><div class="line">    <span class="keyword">this</span>.currentImage = <span class="keyword">this</span>.image;</div><div class="line">  &#125;,</div><div class="line">  <span class="comment">//通过props来获取父组件传递过来的值</span></div><div class="line">  props: &#123;</div><div class="line">    <span class="attr">size</span>: &#123;</div><div class="line">      <span class="attr">type</span>: <span class="built_in">Number</span>,</div><div class="line">      <span class="attr">default</span>: <span class="number">2</span></div><div class="line">    &#125;,</div><div class="line">    <span class="attr">image</span>: &#123;</div><div class="line">      <span class="attr">type</span>: <span class="built_in">String</span>,</div><div class="line">      <span class="attr">default</span>: <span class="string">''</span></div><div class="line">    &#125;</div><div class="line">  &#125;,</div><div class="line">  <span class="attr">watch</span>: &#123;</div><div class="line">    currentImage(newVal) &#123;</div><div class="line">      <span class="keyword">this</span>.$emit(<span class="string">'imageChange'</span>, newVal)</div><div class="line">    &#125;</div><div class="line">  &#125;,</div><div class="line">  mounted() &#123;</div><div class="line">    <span class="comment">//一个通过腾讯cos上传的上传文件的方法</span></div><div class="line">    <span class="keyword">this</span>.$nextTick(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</div><div class="line">      <span class="keyword">let</span> that = <span class="keyword">this</span>;</div><div class="line">      <span class="keyword">this</span>.utils.startCosUpload(&#123;</div><div class="line">        <span class="attr">maxSize</span>: <span class="keyword">this</span>.size,</div><div class="line">        success(result) &#123;</div><div class="line">          <span class="keyword">let</span> imgPath = result.data.access_url.replace(<span class="string">'file'</span>, <span class="string">'picsh'</span>);</div><div class="line">          that.currentImage = imgPath;</div><div class="line">          that.uploadStatus = <span class="string">'success'</span>;</div><div class="line">          setTimeout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">            that.uploadLoading = <span class="literal">false</span>;</div><div class="line">          &#125;, <span class="number">500</span>);</div><div class="line">        &#125;,</div><div class="line">        error(result) &#123;</div><div class="line">          that.$message.error(result.responseText || <span class="string">'error'</span>);</div><div class="line">        &#125;,</div><div class="line">        progress(curr) &#123;</div><div class="line">          that.uploadStatus = <span class="string">''</span>;</div><div class="line">          that.uploadLoading = <span class="literal">true</span>;</div><div class="line">          that.uploadPercent = <span class="built_in">parseInt</span>(curr * <span class="number">100</span>);</div><div class="line">        &#125;</div><div class="line">      &#125;)</div><div class="line">    &#125;);</div><div class="line">  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="子组件与父组件的通信"><a href="#子组件与父组件的通信" class="headerlink" title="子组件与父组件的通信"></a>子组件与父组件的通信</h3><p>需求:在子组件中,我们通过点击按钮,进行了图片的上传操作,现需将上传完成的图片<code>url</code>数据回填给父组件,同时,通过父组件控制子组件中上传图片的类型,大小,作为校验参数!</p>
<p>想要解决以上的问题,我们需要理解清楚几个概念.首先,理解单向数据流:</p>
<blockquote>
<p>prop 是单向绑定的：当父组件的属性变化时，将传导给子组件，但是不会反过来。这是为了防止子组件无意修改了父组件的状态——这会让应用的数据流难以理解。</p>
</blockquote>
<p>Tips:这里强行修改父组件的数据会抛出:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">Avoid mutating a prop directly since the value will be overwritten </div><div class="line">whenever the parent component re-renders</div></pre></td></tr></table></figure>
<p>其次,理解自定义事件:</p>
<p>每个<code>vue</code>实例都实现了事件接口,可使用<code>$on(eventName)</code>来监听事件,<code>$emit(eventName)</code>来触发事件.</p>
<p>回到上面的需求,在父组件中我们通过<code>imageChange</code>这样一个自定义的事件,来监听子组件中图片<code>url</code>的变化,对应子组件中我们通过<code>watch</code>方法观察<code>currentImage</code>的变化,一旦<code>currentImage</code>改变,我们通过<code>$emit</code>来触发<code>imageChange</code>方法,将改变后图片地址传递出去,父组件通过<code>handlImageChange</code>来接收响应子组件的事件:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">handleImageChange(data) &#123;</div><div class="line">  <span class="keyword">this</span>.form.img = data;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="关于异步组件"><a href="#关于异步组件" class="headerlink" title="关于异步组件"></a>关于异步组件</h3><p>需求:我们现在需要通过一个请求获取对应的数据,从而可以通过父组件向子组件中传递请求后的数据,否则,子组件无法获取对应的数据.</p>
<ul>
<li><a href="https://cn.vuejs.org/v2/guide/components.html#异步组件" target="_blank" rel="external">官网关于异步组件</a></li>
<li><a href="http://blog.csdn.net/qq20004604/article/details/56496597" target="_blank" rel="external">通俗的解释</a></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">export</span> <span class="keyword">default</span>&#123;</div><div class="line">  ...</div><div class="line">  components: &#123;</div><div class="line">    commonUploader(resolve) &#123;</div><div class="line">      <span class="comment">//异步组件写法</span></div><div class="line">      setTimeout(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</div><div class="line">        <span class="built_in">require</span>([<span class="string">'@/views/layout/common-uploader'</span>], resolve)</div><div class="line">      &#125;, <span class="number">500</span>);</div><div class="line">    &#125;</div><div class="line">  &#125;,</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h4 id="关于异步组件的问题-尚未找到解决方案"><a href="#关于异步组件的问题-尚未找到解决方案" class="headerlink" title="关于异步组件的问题(尚未找到解决方案)"></a>关于异步组件的问题(尚未找到解决方案)</h4><p>在异步组件中如何调用请求,从而确定组件是在请求完成后才加载的,更好的控制子组件加载的时机,这应该是上述问题的最佳解决方案.<br>但是组件方法<code>commonUploader</code>中<code>this</code>是<code>undefined</code>,所以暂时没找到如何去调用请求.</p>
<h5 id="补充-2017-6-27-18-24-13"><a href="#补充-2017-6-27-18-24-13" class="headerlink" title="补充(2017-6-27 18:24:13)"></a>补充(2017-6-27 18:24:13)</h5><p>使用<code>setTimeout</code>明显是解决不了问题的,会有<code>bug</code>产生.一个补充的解决方案:</p>
<p>通过在父组件中获取子组件的引用,然后在请求完成后直接通过引用来改变子组件的属性.</p>
<p>举个例子:</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">common-uploader</span> <span class="attr">ref</span>=<span class="string">"commonUploader"</span>&gt;</span><span class="tag">&lt;/<span class="name">common-uploader</span>&gt;</span></div></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">...</div><div class="line">request().then(<span class="function"><span class="params">result</span>=&gt;</span>&#123;</div><div class="line">  <span class="keyword">if</span>(result.success)&#123;</div><div class="line">    <span class="keyword">this</span>.$refs.commonUploader.currentImage = result.data;<span class="comment">//直接在请求完成后改变子组件的属性</span></div><div class="line">  &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure>

                <hr>
                <ul class="pager">
                    
                    <li class="previous">
                        <a href="/2017/06/28/2017-note/21.深入理解javascript-50-5/" data-toggle="tooltip" data-placement="top"
                           title="Review深入理解Javascript系列(五)">&larr; 上一页</a>
                    </li>
                    
                    
                    <li class="next">
                        <a href="/2017/06/19/2017-note/19.Fabricjs/" data-toggle="tooltip" data-placement="top"
                           title="fabric.js文档">下一页 &rarr;</a>
                    </li>
                    
                </ul>
                <div class="comment">
                    <div id="cloud-tie-wrapper" class="cloud-tie-wrapper"></div>
                </div>
            </div>
            <div class="hidden-xs col-sm-3 toc-col">
                <div class="toc-wrap">
                    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#父组件向子组件传递值"><span class="toc-text">父组件向子组件传递值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#子组件与父组件的通信"><span class="toc-text">子组件与父组件的通信</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#关于异步组件"><span class="toc-text">关于异步组件</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#关于异步组件的问题-尚未找到解决方案"><span class="toc-text">关于异步组件的问题(尚未找到解决方案)</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#补充-2017-6-27-18-24-13"><span class="toc-text">补充(2017-6-27 18:24:13)</span></a></li></ol></li></ol></li></ol>
                </div>
            </div>
        </div>

        <div class="row">
            <!-- Sidebar Container -->

            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                sidebar-container">

                <!-- Featured Tags -->
                

                <!-- Friends Blog -->
                
            </div>
        </div>

    </div>
</article>
<!-- 网易云音乐-->

<iframe class="cloud-music" frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 
    src="//music.163.com/outchain/player?type=2&amp;id=18803411&amp;auto=0&amp;height=66">
</iframe>
<!-- 网易云跟帖-->
<script src="https://img1.cache.netease.com/f2e/tie/yun/sdk/loader.js"></script>
<script>
var cloudTieConfig = {
  url: document.location.href, 
  sourceId: "",
  productKey: "40a8ee22b6084862a8907e6902e525fa",
  target: "cloud-tie-wrapper"
};
var yunManualLoad = true;
Tie.loader("aHR0cHM6Ly9hcGkuZ2VudGllLjE2My5jb20vcGMvbGl2ZXNjcmlwdC5odG1s", true);
</script>

<!-- Footer -->
<!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1 text-center">
                <p class="copyright text-muted">
                    Copyright &copy; MillZhang 2017
                    <br>
                </p>
            </div>
        </div>
    </div>
</footer>

<!-- jQuery -->
<script src="/js/jquery.min.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="/js/bootstrap.min.js"></script>

<!-- Custom Theme JavaScript -->
<script src="/js/blog.js"></script>

<!-- async load function -->
<script>
    function async(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>

<!-- jquery.tagcloud.js -->
<script>
    // only load tagcloud.js in tag.html
    if($('#tag_cloud').length !== 0){
        async("http://oritfw5nq.bkt.clouddn.com/jquery.tagcloud.js",function(){
            $.fn.tagcloud.defaults = {
                //size: {start: 1, end: 1, unit: 'em'},
                color: {start: '#bbbbee', end: '#0085a1'},
            };
            $('#tag_cloud a').tagcloud();
        })
    }
</script>

<!--fastClick.js -->
<script>
    async("//cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js", function(){
        var $nav = document.querySelector("nav");
        if($nav) FastClick.attach($nav);
    })
</script>



<!--wechat title img-->
<img class="wechat-title-img" src="http://oritfw5nq.bkt.clouddn.com/avatar.jpg?imageslim">
<a id="backtop" class="border-bottom" href="javascript:;">回顶部</a>
<script type="application/javascript">
var docOuter = document.body.scrollTop ? document.body : document.documentElement;
var backToTop = function(rate) {
  var doc = document.body.scrollTop ? document.body : document.documentElement;
  var scrollTop = doc.scrollTop;
  var top = function() {
    scrollTop = scrollTop + (0 - scrollTop) / (rate || 2);
    if (scrollTop < 1) {
      doc.scrollTop = 0;
      return;
    }
    doc.scrollTop = scrollTop;
    // 动画gogogo!
    requestAnimationFrame(top);
  };
  top();
};
var topBtn = document.getElementById('backtop'),
    docTop = docOuter.scrollTop;
if(docTop>=300){
  topBtn.style.opacity = 1;
}

topBtn.onclick = function() {
  backToTop(12)
}
window.onscroll = function(e) {
  var doc = document.body.scrollTop ? document.body : document.documentElement;
  var scrollTop = doc.scrollTop;
  if (scrollTop >= 300) {
    topBtn.style.opacity = 1;
  } else {
    topBtn.style.opacity = 0;
  }
}
</script>
</body>

</html>
